package com.yhy.wxzn.wcs.device.plc.bean;

import com.yhy.wxzn.common.utils.SpringUtil;
import com.yhy.wxzn.wcs.device.rgv.enums.Action;
import com.yhy.wxzn.wcs.device.rgv.enums.PalletStatus;
import com.yhy.wxzn.wcs.device.rgv.enums.ReversingDirection;
import lombok.Data;
import org.jeecg.common.util.RedisUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: 97354
 * @Date: 2023/11/11 15:03
 * @Description: FourCarPlc
 * @Version 1.0.0
 */
@Data
public class FourCarPlc extends BasePlc {

    private volatile long receiveTime;

    /**
     * 四向车编号
     */
    private volatile String carNo;
    /**
     * 四向车目前状态1为空闲，0为工作中
     * */
    private volatile int carStatus;

    public static String getCarStatusStr(int carStatus){
        if (carStatus == 1) return "空闲";
        if (carStatus == 0) return "工作中";


        return "未知";
    }
    /**
     * 电池电量0-100（电量百分比）
     * */
    private volatile int dcdl;
    /**
     * 显示所有报警情况（随后附表格）
     * */
    private volatile int errorType;

    private static Map<Integer,String> errorMap;

    static {
        errorMap = new HashMap<>();
        errorMap.put(0,"驱动伺服报警");
        errorMap.put(1,"顶升/换向伺服报警");
        errorMap.put(2,"里程超出报警");
        errorMap.put(3,"位置偏差过大报警");
        errorMap.put(4,"碰撞故障报警");
        errorMap.put(5,"急停故障报警");
        errorMap.put(6,"低电量报警");
        errorMap.put(7,"相机通讯错误报警");
    }
    /**
     * 二维码数值
     * */
    private volatile String twoCode;
    /**
     * X偏移量
     * */
    private volatile int xvar;
    /**
     * Y偏移量
     * */
    private volatile int yvar;
    /**
     * 电池电压
     * */
    private volatile int dcdy;
    /**
     * 电池温度
     * */
    private volatile int dcwd;
    /**
     * 激光测距传感器
     * */
    private volatile int jgcjcgq;
    /**
     * 四向车心跳保护一直上传1
     * */
    private volatile int heartBeat;
    /**
     * 当前升降位置
     * 1：上位置；2：中位值；3：下位置
     * */
    private volatile int position;


    /**
     * 获取RGV的托盘状态
     * @param
     * @return
     */
    public PalletStatus getPalletStatus(){
        int curPosition = position;
        if (curPosition == Action.顶升.getCmd()) return PalletStatus.UP;
        if (curPosition == Action.下降或副通道.getCmd()) return PalletStatus.DOWN;
        if (curPosition == Action.主通道.getCmd()) return PalletStatus.MAIN_CHANNEL;


        return PalletStatus.UNKNOWN;
    }


    /**
     * 获取RGV的托盘状态
     * @param position
     * @return
     */
    public  ReversingDirection getReversingDirection(){
        int curPosition = position;
        if (curPosition == Action.下降或副通道.getCmd()) return ReversingDirection.副通道;
        if (curPosition == Action.主通道.getCmd()) return ReversingDirection.主通道;
        if (curPosition == Action.顶升.getCmd()) return ReversingDirection.顶升副通道;


        return ReversingDirection.未知;
    }
    /**
     * 升降状态
     * 1：上升中；2：下降中
     * */
    private volatile int sjzt;
    /**
     * 当前里程完成标志 1：当前里程完成
     * */
    private volatile int beFinish;
    /**
     * 当前运行速度
     * */
    private volatile int speed;
    /**
     * 充电状态
     * 1：在充电状态：0：未充电状态
     * */
    private volatile int cdzt;
    /**
     * 当前运行方向
     * （从小车电池方向看过去）1左2右3上4下
     * */
    private volatile int direction;
    /**报警描述*/
    private volatile String remark;
    /**
     *读取任务号
     * **/
    private volatile int readTaskNo;

    private volatile int taskNo;

    //-------------------------------------------------------四向车写入信息------------------------------------------------------------
    /**
     * 行驶里程
     * */
    private volatile int wXslc;
    /**
     * 二维码
     * */
    private volatile List<Integer> wCodes;
    /**
     *X轴绝对值坐标
     * */
    private volatile  List<Integer> wXs;
    /**
     *Y轴绝对值坐标
     * */
    private volatile  List<Integer> wYs;
    /**
     *速度
     * */
    private volatile  int wSpeed;
    /**
     *运行方向
     * */
    private volatile  int wRunAway;
    /**
     *启动信号
     * */
    private volatile  int wStart;
    /**
     *1,顶升。2副通道。3主通道
     * */
    private volatile  int wUpdown;
    /**
     *心跳
     * */
    private volatile  int wHeart;
    /**
     *1：开始充电，2：充电结束
     * */
    private volatile  int wChargeOrder;
    /**
     * 任务号
     */
    private volatile int wTaskNo;



    private volatile  int chargOrder;
    /**
     *写入任务号
     * **/
    private volatile int writeTaskNo;
    /**
     * 四向车当前坐标
     * */
    private volatile String currentCoord;

    public String getErrorMessage(){
        int errType = this.errorType;
        if (errType == 0) return "无故障";
        int mask = 1;
        for (int i = 0; i < 8; i++, mask <<= 1)
            if ((errType & mask) == mask)
                return errorMap.get(i);
        return "发生未知错误";
    }



    private static final String RGV_TASK_NO = "RGV_TASK_NO";

    public synchronized static int getAutoIncrementRgvTaskNo(){
        RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);
        if (redisUtil.get(RGV_TASK_NO) == null || (int)redisUtil.get(RGV_TASK_NO) == 255){
            redisUtil.set(RGV_TASK_NO,1);
            return 1;
        }
        return (int) redisUtil.incr(RGV_TASK_NO, 1);
    }





}
